package com.mdp.im.core.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mdp.im.core.entity.ImPrichatMessage;
import com.mdp.im.core.publish.api.Publisher;
import com.mdp.im.core.service.ImPrichatMessageService;
import com.mdp.im.core.utils.msg.Action;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mdp.core.service.BaseService;

import com.mdp.im.core.entity.ImUserFrend;
import com.mdp.im.core.mapper.ImUserFrendMapper;
/**
 * @author 唛盟开源 code-gen
 * @since 2024-8-14
 */
@Service
public class ImUserFrendService extends BaseService<ImUserFrendMapper,ImUserFrend> {
	static Logger logger =LoggerFactory.getLogger(ImUserFrendService.class);

	@Autowired
	Publisher publisher;
	@Autowired
	private ImPrichatMessageService imPrichatMessageService;

	/**
	 * 自定义查询，支持多表关联
	 * @param page 分页条件
	 * @param ew 一定要，并且必须加@Param("ew")注解
	 * @param ext 如果xml中需要根据某些值进行特殊处理，可以通过这个进行传递，非必须，注解也可以不加
	 * @return
	 */
	public List<Map<String,Object>> selectListMapByWhere(IPage page, QueryWrapper ew, Map<String,Object> ext){
		return baseMapper.selectListMapByWhere(page,ew,ext);
	}





	/** 请在此类添加自定义函数 */
//同意添加好友
	public void insertEachOther(ImUserFrend imUserFrend) {
		String frendUserid=imUserFrend.getFrendUserid();
		String userid=imUserFrend.getUserid();
//搜索原来是否朋友,是就改成已读,不是就添加好友,也改成已读
		Map<String, Object> mmap = new HashMap<>();//修改read
		mmap.put("sendUserid",frendUserid);
		mmap.put("toUserid",userid);
		Map<String, Object> map = new HashMap<>();//查找是否已经是好友
		map.put("frendUserid",frendUserid);
		map.put("userid",userid);
		List<ImUserFrend> list = this.listByMap(map);
		//是朋友
		if(list.size()>0){
			//修改已读
			imPrichatMessageService.editSendFrendToRead(mmap);
		}else{
			//建立我和他的好友
			this.insert(imUserFrend);
			//建立他和我的好友
			imUserFrend.setId(this.createKey("id"));
			imUserFrend.setFrendUserid(userid);
			imUserFrend.setUserid(frendUserid);
			this.insert(imUserFrend);
			//修改已读
			imPrichatMessageService.editSendFrendToRead(mmap);
		}
	}

	//拒绝添加好友
	public void refusAddImUserFrend(ImUserFrend imUserFrend) {
		String frendUserid=imUserFrend.getFrendUserid();
		String userid=imUserFrend.getUserid();
		Map<String, Object> mmap = new HashMap<>();//修改read
		mmap.put("sendUserid",frendUserid);
		mmap.put("toUserid",userid);
		imPrichatMessageService.editSendFrendToRead(mmap);
	}


	//添加好友后打招呼
	public void afterAddImUserFrend(Map<String,Object> pa) {
		String frendUserid=(String) pa.get("frendUserid");
		String userid=(String) pa.get("userid");

		//加完好友提示消息
		JSONObject workerfenpeiMessage=new JSONObject();
		workerfenpeiMessage.put("action", Action.NEW_MESSAGE);
		workerfenpeiMessage.put("channelId", "");
		workerfenpeiMessage.put("sendUserid", userid);
		workerfenpeiMessage.put("toUserid", frendUserid);
		workerfenpeiMessage.put("groupId", userid+"_"+frendUserid);
		workerfenpeiMessage.put("store", "1");
		workerfenpeiMessage.put("msgType", "prichat");
		workerfenpeiMessage.put("sendContent", "我们已经是好友了");
		workerfenpeiMessage.put("sendTime", new Date().getTime());
		publisher.publishPrichatMessage(frendUserid,workerfenpeiMessage.toJSONString());
		//反
		workerfenpeiMessage.put("sendUserid", frendUserid);
		workerfenpeiMessage.put("toUserid", userid);
		workerfenpeiMessage.put("groupId", frendUserid+"_"+userid);
		publisher.publishPrichatMessage(userid,workerfenpeiMessage.toJSONString());
	}

	//添加群聊后打招呼
	public void afterAddGroup(Map<String,Object> pa) {
		//ChannelsHolder.addChannelToGroup((String) groupId, channel);
		JSONObject groupMessage = new JSONObject();
		String sendUsername = (String)pa.get("username");
		String sendUserid = (String)pa.get("userid");
		String groupId= (String)pa.get("groupId");
		groupMessage.put("sendUserid", sendUserid);
		groupMessage.put("sendUsername", sendUsername);
		groupMessage.put("groupId", groupId);
		groupMessage.put("sendContent",  "加入群聊");
		groupMessage.put("action", "newMessage");
		groupMessage.put("msgType", "group");
		groupMessage.put("channelId", "");
		groupMessage.put("sendTime", new Date());

		publisher.publishGroupMessage((String) groupId, groupMessage.toJSONString());
	}
}

